package cn.broadsense.web.controller.biz;

import cn.broadsense.biz.domain.TagRule;
import cn.broadsense.biz.dto.RuleTreeDto;
import cn.broadsense.biz.query.RuleQuery;
import cn.broadsense.biz.service.TagRuleService;
import cn.broadsense.biz.vo.TagRuleVo;
import cn.broadsense.common.annotation.Anonymous;
import cn.broadsense.common.core.controller.BaseController;
import cn.broadsense.common.core.domain.R;
import cn.broadsense.common.core.page.TableDataInfoResult;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 点位数据
 *
 * @author: pengxin
 * @create: 2023-09-12 11:19
 * @Version 1.0
 **/
@RestController
@Api(tags = "标签规则")
@RequestMapping("/tag/rule")
public class TagRuleController extends BaseController {


    @Resource
    private TagRuleService tagRuleService;


    @PreAuthorize("@ss.hasPermi('biz:tagRule:list')")
    @GetMapping("/list")
    @ApiOperation("获取标签规则列表")
    public R<TableDataInfoResult<List<TagRule>>> list(@ApiParam(value = "规则名称") String ruleName,
                                                      @ApiParam(value = "开始时间 (格式 yyyy-MM-dd HH:mm：ss)") String beginTime,
                                                      @ApiParam(value = "结束时间 (格式 yyyy-MM-dd HH:mm：ss)") String endTime) {

        LambdaQueryWrapper<TagRule> query = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(ruleName)) {
            query.like(TagRule::getRuleName, ruleName);
        }
        if (StrUtil.isNotBlank(beginTime)) {
            query.between(TagRule::getCreateTime, beginTime, endTime);
        }
        startPage();
        List<TagRule> list = tagRuleService.list(query);
        return R.ok(getDataTableResult(list));
    }

    @PreAuthorize("@ss.hasPermi('biz:tagRule:list')")
    @GetMapping("/listAll")
    @ApiOperation("获取所有标签列表")
    public R<List<TagRule>> listAll() {
        LambdaQueryWrapper<TagRule> query = new LambdaQueryWrapper<>();
        query.eq(TagRule::getStatus,1);
        List<TagRule> list = tagRuleService.list(query);
        return R.ok(list);
    }

    @PreAuthorize("@ss.hasPermi('biz:tagRule:add')")
    @PostMapping
    @ApiOperation("添加标签规则")
    public R addTagRule(@RequestBody @Validated TagRuleVo tagRuleVo) {
        tagRuleService.insertRule(tagRuleVo);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('biz:tagRule:remove')")
    @DeleteMapping("/{id}")
    @ApiOperation("删除标签规则")
    public R delete(@PathVariable Long id) {
        Long childId = tagRuleService.getChildrenList(id);
        if(Objects.nonNull(childId)){
            return R.fail("该规则下有子规则，无法删除");
        }
        tagRuleService.removeById(id);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('biz:tagRule:edit')")
    @PutMapping("/{id}")
    @ApiOperation("更新标签规则")
    public R update(@PathVariable Long id, @RequestBody @Validated TagRuleVo tagVo) {
        tagRuleService.editRule(id, tagVo);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('biz:tagRule:list')")
    @GetMapping("/treeList")
    @ApiOperation("获取标签规则树列表")
    public R<List<RuleTreeDto>> treeList(RuleQuery query) {
        return R.ok(tagRuleService.getRuleTree(query));
    }

    @PreAuthorize("@ss.hasPermi('biz:tagRule:list')")
    @GetMapping("/treeOption")
    @ApiOperation("获取标签规则选项树列表")
    public R<List<RuleTreeDto>> treeOption() {
        return R.ok(tagRuleService.getRuleOption());
    }

}
